# -* - coding: UTF-8 -* -
#import sys
#reload(sys) 
#sys.setdefaultencoding('utf8')
#usage: 启动: python3 coop_d.py start
#	   关闭: python3 coop_d.py stop
#	   状态: python3 coop_d.py status
#	   重启: python3 coop_d.py restart
#	   查看: ps -axj | grep coop_d
import os
import sys
import coop
from daemon import Daemon
from config import Config

class CoopDaemon(Daemon):
	def doCreate(self):
		self.coop = coop.Coop()
	def doStart(self):
		self.coop.run()
	def doStop(self):
		return self.coop.terminate()

if __name__ == '__main__':
	conf_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'coop.conf')
	config = Config(conf_path)
	pid = config.get('pid', 'name', '/tmp/coop_houjiji_daemon.pid')
	daemon = CoopDaemon(pid)#,'/dev/stdin','/dev/stdout', '/dev/stderr')
	if len(sys.argv) == 2:
		if 'start' == sys.argv[1]:
			daemon.start()
		elif 'stop' == sys.argv[1]:
			daemon.stop()
		elif 'restart' == sys.argv[1]:
			daemon.restart()
		elif sys.argv[1] == 'status':
			daemon.is_running()
		else:
			print('Unknown command')
			sys.exit(2)
		sys.exit(0)
	else:
		print('usage: %s start|stop|restart' % sys.argv[0])
		sys.exit(2)
